package cn.hotel.servlet;

import cn.hotel.pojo.HouseType;
import cn.hotel.pojo.OrderManagement;
import cn.hotel.service.OrderFormService;
import cn.hotel.service.OrderFormServiceImpl;
import cn.hotel.utlis.PageBean;
import cn.hotel.utlis.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@WebServlet("/orderForm")
public class OrderFormServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out  = response.getWriter();
        OrderFormService orderFormService = new OrderFormServiceImpl();
        String type = request.getParameter("type");
        type = type == null ? "seek" : type;
        switch (type){
            //展示房型
            case "show" :
                show(request,response,out, orderFormService);
                break;
            //展示所以信息 根据房型
            case "seek" :
                seek(request,response, orderFormService);
                break;
            //退房 根据房号修改状态
            case "checkOut" :
                checkOut(request,response,out, orderFormService);
                break;
            //新增订单
            case "addResident" :
                addResident(request,response,out, orderFormService);
                break;
                //查询指定信息 根据房间号
            case "check" :
                check(request,response,out,orderFormService);
                break;
                //修改入住信息 根据房间id
            case "revamp" :
                revamp(request,response,out,orderFormService);
                break;
                //删除订单 根据房间id
            case "delete" :
                delete(request,response,out,orderFormService);
            default:
                break;
        }
    }

    //删除订单 根据房间id
    private void delete(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        Long id = 0l;
        try {
            id = new Long(request.getParameter("id"));
        }catch (Exception e){}
        int roomNumber = 0;
        try {
            roomNumber = Integer.parseInt(request.getParameter("roomNumber"));
        }catch (Exception e){}
        Map<String,Object> deleteMap = orderFormService.delete(id);
        int setState = orderFormService.setState(0,roomNumber);
        if ("1".equals(deleteMap.get("flag")) && setState == 1){
            out.print("<script>location.href='"+request.getContextPath()+"/orderForm';</script>");
        }else {
            out.print("<script>alert('删除失败!');location.href='"+request.getContextPath()+"/orderForm';</script>");
        }
    }

    //修改入住信息 根据房间id
    private void revamp(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException {
        OrderManagement orderManagement = new OrderManagement();
        String manageName = request.getParameter("manageName");
        orderManagement.setManageName(manageName);
        String checkOutDate = request.getParameter("checkOutDate");
        orderManagement.setCheckOutDate(checkOutDate);
        String mobilePhone = request.getParameter("mobilePhone");
        orderManagement.setMobilePhone(mobilePhone);
        String identityCard = request.getParameter("identityCard");
        orderManagement.setIdentityCard(identityCard);
        int personNum = 0;
        try {
            personNum = Integer.parseInt(request.getParameter("personNum"));
        }catch (Exception e){}
        orderManagement.setPersonNum(personNum);
        int houseTypeId = 0;
        try {
            houseTypeId = Integer.parseInt(request.getParameter("houseTypeId"));
        }catch (Exception e){}
        orderManagement.setHouseTypeId(houseTypeId);
        String price = request.getParameter("price");
        orderManagement.setPrice(price);
        int roomNumber = 0;
        try {
            roomNumber = Integer.parseInt(request.getParameter("roomNumber"));
        }catch (Exception e){}
        orderManagement.setRoomNumber(roomNumber);
        double discount = 0;
        try {
            discount = Integer.parseInt(request.getParameter("discount"));
        }catch (Exception e){}
        orderManagement.setDiscount(discount);
        String remark = request.getParameter("remark");
        orderManagement.setRemark(remark);
        Long id = 0l;
        try {
            id = new Long(request.getParameter("id"));
        }catch (Exception e){}
        int condition = 0;
        try {
            condition = Integer.parseInt(request.getParameter("condition"));
        }catch (Exception e){}
        orderManagement.setId(id);
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Map<String,Object> revampMap = orderFormService.revamp(orderManagement);
        int setState = orderFormService.setState(condition,roomNumber);
        if ("1".equals(revampMap.get("flag")) && setState == 1){
            out.print("<script>location.href='"+request.getContextPath()+"/orderForm';</script>");
        }else {
            out.print("<script>alert('修改失败!');location.href='"+request.getContextPath()+"/orderForm';</script>");
        }
    }

    //查询指定信息 根据房间号
    private void check(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        int roomNumber = 0;
        try {
            roomNumber = Integer.parseInt(request.getParameter("roomNumber"));
        }catch (Exception e){}

        OrderManagement check = orderFormService.check(roomNumber);
        List<HouseType> showList = orderFormService.show();
//        Map<String,Object> setState = orderFormService.setState(0,roomNumber);
//        if ("1".equals(setState.get("flag"))){
            request.setAttribute("check",check);
            request.setAttribute("showList",showList);
            request.getRequestDispatcher("orderFormjsp/redact.jsp").forward(request,response);
//        }
    }

    //新增订单
    private void addResident(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        OrderManagement orderManagement = new OrderManagement();
        //接收房型
        int houseTypeId = 0;
        try {
            houseTypeId = Integer.parseInt(request.getParameter("houseTypeId"));
        }catch (Exception e){}
        orderManagement.setHouseTypeId(houseTypeId);
        //入住状态
        int condition = 0;
        try {
            condition = Integer.parseInt(request.getParameter("condition"));
        }catch (Exception e){}
        //接收房号
        int roomNumber = 0;
        try {
            roomNumber = Integer.parseInt(request.getParameter("roomNumber"));
        }catch (Exception e){}
        orderManagement.setRoomNumber(roomNumber);
        //接收居住人数
        int personNum = 0;
        try {
            personNum = Integer.parseInt(request.getParameter("personNum"));
        }catch (Exception e){}
        orderManagement.setPersonNum(personNum);
        //接收姓名
        String manageName = request.getParameter("manageName");
        orderManagement.setManageName(manageName);
        //接收手机号
        String mobilePhone = request.getParameter("mobilePhone");
        orderManagement.setMobilePhone(mobilePhone);
        //接收身份证号
        String identityCard = request.getParameter("identityCard");
        orderManagement.setIdentityCard(identityCard);
        //接收入住时间
        String checkInDate = request.getParameter("checkInDate");
        orderManagement.setCheckInDate(checkInDate);
        //接收退房时间
        String checkOutDate = request.getParameter("checkOutDate");
        orderManagement.setCheckOutDate(checkOutDate);
        //接收备注
        String remark = request.getParameter("remark");
        orderManagement.setRemark(remark);
        //接收订单号
        String password = request.getParameter("password");
        orderManagement.setPassword(password);
        //接收金额
        String price = request.getParameter("price");
        orderManagement.setPrice(price);
        //调用添加客户方法，传入参数
        int addClient = orderFormService.addClient(orderManagement);
        int setStateMap = orderFormService.setState(condition,roomNumber);
        if (addClient == 1 && setStateMap == 1){
            out.print("<script>location.href='"+request.getContextPath()+"/orderForm';</script>");
        }else {
            out.print("<script>alert('添加失败!');location.href='"+request.getContextPath()+"/orderForm';</script>");
        }
    }

    //退房 根据房号修改状态
    private void checkOut(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        //接收房间id
        int roomNumber = 0;
        try {
            roomNumber = Integer.parseInt(request.getParameter("roomNumber"));
        }catch (Exception e){}
        //修改入住状态
        int checkOutMap = orderFormService.setState(-1,roomNumber);
        Map<String,Object> setCheckOutDate = orderFormService.setCheckOutDate(roomNumber);
        if ( checkOutMap == 1 && "1".equals(setCheckOutDate.get("flag"))){
            out.print("<script>location.href='"+request.getContextPath()+"/orderForm';</script>");
        }else {
            out.print("<script>alert('退房失败!');location.href='"+request.getContextPath()+"/orderForm';</script>");
        }
    }

    //展示所以信息 根据房型
    private void seek(HttpServletRequest request, HttpServletResponse response, OrderFormService orderFormService) throws ServletException ,IOException{
        PageBean<OrderManagement> paging = new PageBean<>();
        //房型
        int houseTypeId;
        try {
            houseTypeId = Integer.parseInt(request.getParameter("houseTypeId"));
        }catch (Exception e){
            houseTypeId = 0;
        }
        //入住状态
        int condition = 0;
        try {
            condition = Integer.parseInt(request.getParameter("condition"));
        }catch (Exception e){}
        //入住房间
        int roomNumber = 0;
        try {
            roomNumber = Integer.parseInt(request.getParameter("roomNumber"));
        }catch (Exception e){}
        //客户名字
        String manageName = request.getParameter("manageName");
        manageName = StringUtil.isEmpty(manageName) ? "" : manageName;
        //当前页
        int index = 1;
        try {
            index = Integer.parseInt(request.getParameter("index"));
        }catch (Exception e){}
        //接收查多少数据方法
        int count = orderFormService.count(houseTypeId,condition,roomNumber,manageName);
        //计算分多少页
        paging.setTotalCount(count);
        //接收当前页
        paging.setCurrPageNo(index);
        //所有信息
        List<OrderManagement> seekList = orderFormService.seek(houseTypeId,condition,roomNumber,manageName,paging.getCurrPageNo(),paging.getPageSize());
        //户型
        List<HouseType> showList = orderFormService.show();
        //分页接收
        paging.setProductList(seekList);
        //发送web
        request.setAttribute("showList",showList);
        request.setAttribute("paging",paging);
        request.setAttribute("houseTypeId",houseTypeId);
        request.setAttribute("condition",condition);
        request.setAttribute("roomNumber",roomNumber);
        request.setAttribute("manageName",manageName);
        request.getRequestDispatcher("orderFormjsp/inquireCustomer.jsp").forward(request,response);
    }

    //展示房型
    private void show(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        List<HouseType> showList = orderFormService.show();
        request.setAttribute("showList",showList);
        request.getRequestDispatcher("orderFormjsp/addClient.jsp").forward(request,response);
    }

}
